from time import sleep

from base.base import Base
from page import y_web_ele as e_data


class PageNavigationManager(Base):
    def __init__(self, url):
        super().__init__(url)

    # 点击网站管理链接
    def click_web_manager_a(self):
        self.click(e_data.web_manager_menu)

    # 点击导航管理子菜单链接
    def click_navigation_manager(self):
        self.click(e_data.navigation_manager)

    # 进入iframe
    def to_iframe(self):
        self.go_iframe(e_data.iframe)

    # 点击中间导航
    def click_navigation_centre_navigation(self):
        self.click(e_data.navigation_centre_navigation)

    # 点击底部导航
    def click_navigation_bottom_navigation(self):
        self.click(e_data.navigation_bottom_navigation)

    # 点击新增按钮
    def click_navigation_add_butt(self):
        self.click(e_data.navigation_add_butt)

    """ 自定义导航添加 """

    # 点击添加——自定义导航按钮
    def click_add_custom_butt(self):
        self.click(e_data.navigation_add_custom)

    # 选择导航级别
    def select_custom_rank(self, num):
        self.click(e_data.select_custom_rank)
        self.click(e_data.custom_rank(num))

    # 输入导航名称
    def input_custom_name(self, value):
        self.input(e_data.input_custom_name, value)

    # 输入url地址
    def input_custom_url(self, value):
        self.input(e_data.input_custom_url, value)

    # 输入顺序
    def input_custom_sequence(self, value):
        self.input(e_data.input_custom_sequence, value)

    # 选择是否显示
    def radio_custom_show(self, mark):
        self.click(e_data.custom_radio_show(mark))

    # 选择是否新窗口打开
    def radio_custom_view_new(self, mark):
        self.click(e_data.custom_view_new(mark))

    # 点击提交按钮
    def click_custom_save_butt(self):
        self.click(e_data.add_custom_save_butt)

    # 保存出错后点击返回
    def click_custom_add_error_back(self):
        self.click(e_data.add_custom_error_back)

    """ 文章导航添加 """

    # 点击添加——文章导航按钮
    def click_add_article_butt(self):
        self.click(e_data.navigation_add_article)

    # 选择导航级别
    def select_article_rank(self, num):
        self.click(e_data.select_article_rank)
        self.click(e_data.article_rank(num))

    # 选择文章
    def select_article_choose(self, num):
        self.click(e_data.select_article_choose)
        self.click(e_data.article_choose(num))

    # 输入导航名称
    def input_article_name(self, value):
        self.input(e_data.input_article_name, value)

    # 输入顺序
    def input_article_sequence(self, value):
        self.input(e_data.input_article_sequence, value)

    # 选择是否显示
    def radio_article_show(self, mark):
        self.click(e_data.article_show(mark))

    # 选择是否新窗口打开
    def radio_article_view_new(self, mark):
        self.click(e_data.article_view_new(mark))

    # 点击提交按钮
    def click_article_save_butt(self):
        self.click(e_data.add_article_save_butt)

    # 保存出错后点击返回
    def click_article_add_error_back(self):
        self.click(e_data.add_article_error_back)

    """ 自定义页面导航添加 """

    # 点击添加——自定义页面导航按钮
    def click_add_custom_page_butt(self):
        self.click(e_data.navigation_add_custom_page)

    # 点击添加——商品分类导航按钮
    def click_add_commodity_butt(self):
        self.click(e_data.navigation_add_commodity)

    # 获取保存的提示
    def get_save_info(self):
        return self.get_text(e_data.add_save_info)

    # 点击删除按钮，并确定删除
    def click_navigation_delete_butt(self):
        self.click(e_data.navigation_delete_butt)
        self.click(e_data.navigation_delete_true)

    # 获取tbody的tr
    def get_tbody_tr(self, nums):
        for num in nums:
            self.click(e_data.select_tbody_tr_td(num))

    # 点击搜索按钮
    def click_navigation_search_butt(self):
        self.click(e_data.navigation_search_butt)

    # 点击重置搜索链接
    def click_navigation_reset_a(self):
        self.click(e_data.navigation_reset_a)

    # 添加——自定义导航——流程
    def add_custom_navigation(self, rank, name, url, sequence, is_show, is_new):
        sleep(1)
        self.click_navigation_add_butt()  # 点击添加按钮
        self.click_add_custom_butt()  # 点击自定义添加
        self.select_custom_rank(rank)  # 选择导航级别
        self.input_custom_name(name)  # 输入导航名称
        self.input_custom_url(url)  # 输入url地址
        self.input_custom_sequence(sequence)  # 输入顺序
        self.radio_custom_show(is_show)  # 设置是否显示
        self.radio_custom_view_new(is_new)  # 设置是否通过新界面打开
        self.click_custom_save_butt()  # 点击保存按钮
        sleep(1)
        return self.get_save_info()

    # 添加——文章导航——流程
    def add_article_navigation(self, rank, choose, name, sequence, is_show, is_new):
        sleep(1)
        self.click_navigation_add_butt()  # 点击添加按钮
        self.click_add_article_butt()  # 点击自定义添加
        self.select_article_rank(rank)  # 选择导航级别
        self.select_article_choose(choose)  # 选择文章
        self.input_article_name(name)  # 输入导航名称
        self.input_article_sequence(sequence)  # 输入顺序
        self.radio_article_show(is_show)  # 设置是否显示
        self.radio_article_view_new(is_new)  # 设置是否通过新界面打开
        self.click_article_save_butt()  # 点击保存按钮
        sleep(1)
        return self.get_save_info()

    # 删除——勾选框（多选删除）
    def delete_select(self, nums):
        self.get_tbody_tr(nums)  # 获取tbody的tr，并将需要的勾选
        self.click_navigation_delete_butt()  # 点击删除
        return self.get_save_info()  # 获取删除的操作结果信息

# if __name__ == '__main__':
#     test = PageNavigationManager('http://192.168.8.222/admin.php')
#     test.driver.add_cookie({
#         "name": "PHPSESSID",
#         "value": "8u0i82s9dcncued1nui0b4bo64"
#     })
#     test.driver.refresh()
#     test.click_web_manager_a()
#     test.click_navigation_manager()  # 点击子菜单
#     test.go_iframe(e_data.iframe)  # 进入iframe
#     test.get_tbody_tr([4, 5])
#     test.click(e_data.navigation_delete_butt)
#     test.click(e_data.navigation_delete_true)
#     sleep(5)
#     test.click_navigation_centre_navigation()
#     test.click_navigation_add_butt()
#     test.click_add_custom_butt()
#     test.select_custom_rank(3)
#     test.input_custom_name('测试')
#     test.input_custom_url('https://www.163.com')
#     test.input_custom_sequence('3')
#     test.radio_custom_show(True)
#     test.radio_custom_view_new(False)
#     sleep(3)
#     test.click_custom_save_butt()
#     sleep(5)
